package cn.stronglink.assetmanage.business.inventory.model;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.List;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.InventoryDao;
import cn.stronglink.assetmanage.entity.Inventory;
import cn.stronglink.assetmanage.entity.InventoryDetail;
import cn.stronglink.assetmanage.entity.Result;
import cn.stronglink.assetmanage.entity.Task;
import cn.stronglink.assetmanage.entity.User;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.netrequest.business.inventory.InventoryService;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

import static android.webkit.WebViewDatabase.getInstance;

/**
 * Created by chencongcong on 2018/6/18.
 */

public class InventoryModel {
    private Gson gson = new Gson();
    private onInventoryDataListener onInventoryDataListener;
    private InventoryService inventoryService;

    public InventoryModel() {
        inventoryService = new InventoryService();
    }

    public InventoryModel.onInventoryDataListener getOnInventoryDataListener() {
        return onInventoryDataListener;
    }

    public void setOnInventoryDataListener(InventoryModel.onInventoryDataListener onInventoryDataListener) {
        this.onInventoryDataListener = onInventoryDataListener;
    }

    public void getInventoryData(){
        try {
            inventoryService.downloadInventoryWorkOrder(User.getInstance().getId_(),new Callback() {
                @Override
                public void onFailure(Call call, final IOException e) {
                    if(onInventoryDataListener!=null){
                        onInventoryDataListener.onFailure(call,e);
                    }
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if(onInventoryDataListener!=null){
                        onInventoryDataListener.onResponse(call,response);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void saveInventoryData(String resultJson){
        Result<List<Inventory>> inventoryResult = null;
        inventoryResult = gson.fromJson(resultJson,new TypeToken<Result<List<Inventory>>>(){}.getType());
        if(inventoryResult!=null&&inventoryResult.getHttpCode()== HttpURLConnection.HTTP_OK){
            if(inventoryResult.getData()!=null) {
                List<Inventory> inventoryList = inventoryResult.getData();
                // 验收单列表
                if(inventoryList!=null&&inventoryList.size()>0){
                    //从服务器获取盘点数据，清除本地盘点数据库表
//                    deleteAllInventory();
                    for(Inventory inventory:inventoryList){
                       // 如果盘点单存在，就不添加
                        Inventory tempInventory = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getInventoryDao().queryBuilder().where(
                                InventoryDao.Properties.Id_.eq(inventory.getId_())
                        ).unique();
                        if(tempInventory!=null){
                            continue;
                        }

                        inventory.setOrderType(Constants.STATUS_TWO);
                        //将验收单列表保存在任务表中
//                        Task task = new Task();
//                        task.setId(inventory.getId());
//                        task.setId_(inventory.getId_());
//                        task.setStatus(inventory.getStatus());
//                        task.setBusinessType(Constants.STATUS_TWO);
//                        DaoManager.getInstance(MyApplication.APP_CONTEXT).getDaoSession().getTaskDao().insertOrReplace(task);


                        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getInventoryDao().insert(inventory);
                        // 获取详情
                        if(inventory.getDetailList()!=null&&inventory.getDetailList().size()>0){
                            for (InventoryDetail detail:inventory.getDetailList()){
                                detail.setOrderNo(inventory.getId_());
                                DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getInventoryDetailDao().insert(detail);
                            }
                        }
                    }
                }
            }
        }
    }

    public void deleteAllInventory(){
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getInventoryDetailDao().deleteAll();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getInventoryDao().deleteAll();
    }


    public interface onInventoryDataListener {
        void onFailure(Call call, IOException ex);

        void onResponse(Call call, Response response);
    }
}
